package TAIC.feature;

import java.io.File;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;

/**
 * If run without parameters, the program extract key point in the current folder.
 * Else this program will take in a folder list and extract key points of all the folders in the list
 * @author Kent
 *
 */
public class KeyPointExtract {
	public final int MAX_PART = 40 ;
	double[][][] featureSet;
	double[][][] pos ;
	
	static public void main ( String args[]) throws Exception {
		if ( args.length == 0 ) (new KeyPointExtract()).extractOneFolder(".") ;
		else if ( args.length == 1 )  (new KeyPointExtract()).extractFolders( args[0]) ;
		else System.out.println ( "please input the the filename of folder list") ;
	}
	
	public void extractFolders ( String listFile ) throws Exception {
		Scanner scanner = new Scanner ( new File ( listFile )) ;
		while ( scanner.hasNextLine() ) extractOneFolder ( scanner.nextLine().trim()) ;
		scanner.close(); 		
	}
	
	public void extractOneFolder ( String folder ) throws Exception {
		Vector < String > fileList = new Vector < String > () ; 
		Scanner scanner = new Scanner ( new File ( folder + "\\index.txt" ) ) ;
		while ( scanner.hasNext() )  fileList.add( scanner.nextLine().trim() ) ;
		scanner.close() ;
		
		int n = fileList.size() ;
		int part = n / MAX_PART + 1 ;
		int len = ( n + part - 1 )/ part; 
//		for ( int i = 0 ; i < n ; i ++ ) 
		
		
		for ( int outer = 0 ; outer < part ; outer ++ ) {
			int begin = outer * len ;
			int end = begin + len ; 
			if ( end > n ) end = n ;
			
			//System.out.println ( begin + " " + end ) ; 
			featureSet = new double[n][][] ; 
			pos = new double[n][][] ; 
			for ( int i = begin; i < end; i ++ ) 
				getFeatureSet ( folder + "\\" + fileList.get(i) + ".sift_o", featureSet, pos, i );
			
			for ( int i = begin ; i < end ; i ++ ) {			
				double[][] source = featureSet[ i ] ;  
				double[][] sPos = pos[ i ]; 

				Node matchCount[] = new Node [ source.length ] ;
				for ( int j = 0; j < source.length ; j ++ ) matchCount[ j ] = new Node (j, 0) ;

				for ( int j = begin ; j < end ; j ++ ) {
					System.out.print (".") ;
					if ( j == i ) continue ; 
					double[][] target = featureSet [ j ] ; 
					Match.match(source, target, matchCount) ;
				}

				Arrays.sort( matchCount ) ; 
				PrintStream fout = new PrintStream ( new File ( folder + "\\" + fileList.get(i)+ ".point")) ;
				fout.println ( fileList.get(i)) ;
				for ( int j = 0 ; j < source.length ; j ++ ) { 
					fout.println ( sPos[matchCount[j].index][0] + " "+ sPos[matchCount[j].index][1] + " " + j 
							+ " " + matchCount[j].index +  " " + matchCount[j].value);
				}
				fout.close() ;
				System.out.println ( "Image " + i + " of folder " + folder + " done" ) ; 			
			}
		}
	}
	
	public void getFeatureSet ( String filename, double[][][] featureSet, double[][][] pos, int index ) throws Exception { 
		Scanner scanner = new Scanner ( new File ( filename )) ; 
		int n = scanner.nextInt() ; 
		int m = scanner.nextInt() ;
		featureSet [ index ] = new double[n][m] ;
		pos [ index ] = new double [ n ][ 2 ] ; 
		for ( int i = 0 ; i < n ;i ++ ) {
			for ( int j = 0 ; j < 2 ; j ++ ) pos [index][i][j] = scanner.nextDouble() ;
			scanner.nextDouble() ;scanner.nextDouble() ;
			for ( int j = 0 ; j < m; j ++ ) featureSet[index][i][j] = scanner.nextDouble() ;
		}
		scanner.close();
	}
}